package yunquan.wang.base.dao.springdata;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import yunquan.wang.base.dao.DaoUtils;
/**
 * 此类扩展了Spring 
 * @author Administrator
 *
 * @param <T>
 */
public class BaseRepositoryImpl <T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID>{
/**
 * 
 * @param entityInformation
 * @param entityManager
 */
private 	EntityManager entityManager;
private 	 Class<T>  domainClass;
	public BaseRepositoryImpl(JpaEntityInformation<T, ID> entityInformation,
			EntityManager entityManager) {
		super(entityInformation, entityManager);
		this.entityManager=entityManager;
		this.domainClass=entityInformation.getJavaType();
		
	}

 

	public BaseRepositoryImpl(Class<T> domainClass, EntityManager em) {
		super(domainClass, em);
		this.entityManager=em;
       this.domainClass=domainClass;
	}
 
	
	public EntityManager getEntityManager() {
		return entityManager;
	}



	public Class<T> getDomainClass() {
		return domainClass;
	}



	public List<T>  findList(Collection<String> columns)
	{
		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<T> query = builder.createQuery(domainClass);
	    Root<T> root = query.from(domainClass);
	    List<Selection<?>> pathList=new ArrayList<Selection<?>>();
	    for(String column:columns)
	    {
	    	pathList.add(DaoUtils.toExpressionRecursively(root, column));
	    }
	    query.multiselect(pathList);
	    return entityManager.createQuery(query).getResultList();
	}

	public	Page<T> findAll(Specification<T> spec, Pageable pageable,Collection<String> columns)
	{
		return null;
	}
}
